﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using System.Reflection;

namespace Achievement.Common
{
    /// <summary>
    /// 数据源帮助类
    /// </summary>
    public static class DBHelper
    {
        /// <summary>
        /// 排序
        /// </summary>
        /// <typeparam name="T">实体类型</typeparam>
        /// <param name="source">数据源</param>
        /// <param name="sortExpression">排序的字段</param>
        /// <param name="sortDirection">排序方式</param>
        /// <returns></returns>
        public static IQueryable<T> DataSorting<T>(IQueryable<T> source, string sortExpression, string sortDirection)
        {
            string sortingDir = string.Empty;
            if (sortDirection.ToUpper().Trim() == "ASC")
                sortingDir = "OrderBy";
            else if (sortDirection.ToUpper().Trim() == "DESC")
                sortingDir = "OrderByDescending";
            ParameterExpression param = Expression.Parameter(typeof(T), sortExpression);
            PropertyInfo pi = typeof(T).GetProperty(sortExpression);
            Type[] types = new Type[2];
            types[0] = typeof(T);
            types[1] = pi.PropertyType;
            Expression expr = Expression.Call(typeof(Queryable), sortingDir, types, source.Expression, Expression.Lambda(Expression.Property(param, sortExpression), param));
            IQueryable<T> query = source.AsQueryable().Provider.CreateQuery<T>(expr);
            return query;
        }

        /// <summary>
        /// 分页
        /// </summary>
        /// <typeparam name="T">实体类型</typeparam>
        /// <param name="source">数据源</param>
        /// <param name="pageNumber">页码</param>
        /// <param name="pageSize">每页条数</param>
        /// <returns></returns>
        public static IQueryable<T> DataPaging<T>(IQueryable<T> source, int pageNumber, int pageSize)
        {
            return source.Skip(pageNumber * pageSize).Take(pageSize);
        }

        /// <summary>
        /// 排序加分页
        /// </summary>
        /// <typeparam name="T">实体类型</typeparam>
        /// <param name="source">数据源</param>
        /// <param name="sortExpression">排序的字段</param>
        /// <param name="sortDirection">排序方式</param>
        /// <param name="pageNumber">页码</param>
        /// <param name="pageSize">每页条数</param>
        /// <returns></returns>
        public static IQueryable<T> SortingAndPaging<T>(IQueryable<T> source, string sortExpression, string sortDirection, int pageNumber, int pageSize)
        {
            IQueryable<T> query = DataSorting<T>(source, sortExpression, sortDirection);
            return DataPaging(query, pageNumber, pageSize);
        }

        /// <summary>
        /// 查询指定实体对象中包含某个属性的值
        /// </summary>
        /// <typeparam name="T">实体类型</typeparam>
        /// <param name="options">关键词</param>
        /// <param name="fieldNames">属性</param>
        /// <returns></returns>
        public static Expression<Func<T, bool>> GetConditionExpression<T>(string[] options, string[] fieldNames)
        {
            ParameterExpression left = Expression.Parameter(typeof(T), "c");//c=>          
            Expression expression = Expression.Constant(false);
            foreach (var fieldName in fieldNames)
            {
                foreach (var optionName in options)
                {
                    Expression right = Expression.Call
                   (
                        Expression.Property(left, typeof(T).GetProperty(fieldName)),
                        typeof(string).GetMethod("Contains", new Type[] { typeof(string) }),// 反射使用.Contains()方法 
                        Expression.Constant(optionName)// .Contains(optionName)    
                   );
                    expression = Expression.Or(right, expression);
                }
            }
            Expression<Func<T, bool>> finalExpression = Expression.Lambda<Func<T, bool>>(expression, new ParameterExpression[] { left });
            return finalExpression;
        }
    }
}
